package com.xydtech.ecard.custgroup.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerGroupMerchant;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerGroupMerchantMapper;
import com.xydtech.ecard.custgroup.service.IEcardApiCustomerGroupMerchantService;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantTask;
import com.xydtech.ecard.ecard.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 电子名片客群、社团组织和商户、企业、新型农业主体关联Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-11-27 下午4:20
 */
@Service
public class EcardApiCustomerGroupMerchantServiceImpl implements IEcardApiCustomerGroupMerchantService {

    @Autowired
    private EcardApiCustomerGroupMerchantMapper ecardCustomerGroupMerchantMapper;
    @Autowired
    private EcardApiPersonInfoMapper ecardPersonInfoMapper;
    @Autowired
    private EcardApiMerchantInfoMapper ecardMerchantInfoMapper;
    @Autowired
    private EcardApiEnterpriseInfoMapper ecardEnterpriseInfoMapper;
    @Autowired
    private EcardApiAgricultureInfoMapper ecardAgricultureInfoMapper;
    @Autowired
    private EcardApiMerchantTaskMapper ecardMerchantTaskMapper;

    /**
     * 查询电子名片客群、社团组织和商户、企业、新型农业主体关联
     *
     * @param id 电子名片客群、社团组织和商户、企业、新型农业主体关联主键
     * @return 电子名片客群、社团组织和商户、企业、新型农业主体关联
     */
    @Override
    public EcardApiCustomerGroupMerchant selectEcardCustomerGroupMerchantById(Long id)
    {
        return ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantById(id);
    }

    /**
     * 查询电子名片客群、社团组织和商户、企业、新型农业主体关联列表
     *
     * @param ecardApiCustomerGroupMerchant 电子名片客群、社团组织和商户、企业、新型农业主体关联
     * @return 电子名片客群、社团组织和商户、企业、新型农业主体关联
     */
    @Override
    public List<EcardApiCustomerGroupMerchant> selectEcardCustomerGroupMerchantList(EcardApiCustomerGroupMerchant ecardApiCustomerGroupMerchant)
    {
        return ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantList(ecardApiCustomerGroupMerchant);
    }

    /**
     * 保存行业客户数据接口
     * @param customerGroupMerchant
     * @return
     */
    @Override
    public int addCustomerGroupMerchantInfo(EcardApiCustomerGroupMerchant customerGroupMerchant) {
        return ecardCustomerGroupMerchantMapper.addCustomerGroupMerchantInfo(customerGroupMerchant);
    }

    /**
     * 条件查询登录人客群下的行业客户接口
     * @param ecardCustomerGroupMerchant
     * @return
     */
    @Override
    public List<EcardApiCustomerGroupMerchant> getIndustrialCustomerList(EcardApiCustomerGroupMerchant ecardCustomerGroupMerchant, String salt) {
        List<EcardApiCustomerGroupMerchant> industrialCustomerList = ecardCustomerGroupMerchantMapper.getIndustrialCustomerList(ecardCustomerGroupMerchant);
        if (industrialCustomerList.size() > 0){
            for (EcardApiCustomerGroupMerchant groupMerchant : industrialCustomerList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(groupMerchant.getPhone())) {
                        groupMerchant.setPhone(AESEncryptionUtils.decrypt(groupMerchant.getPhone(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return industrialCustomerList;
    }

    /**
     * 根据id修改行业客户状态
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeIndustrialCustomerStatus(Long id) {
        EcardApiCustomerGroupMerchant groupMerchant = ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantById(id);
        Long merchantId = groupMerchant.getMerchantId();//商户id
        String merchantType = groupMerchant.getMerchantType();//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        //查询是否存在未处理营销任务
        EcardApiMerchantTask merchantTask = ecardMerchantTaskMapper.getMerchantTaskByMerchantId(id, merchantType);//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        if (merchantTask!=null) {
            //存在则更新为已完成
            merchantTask.setTaskSts("1");
            ecardMerchantTaskMapper.updateEcardMerchantTask(merchantTask);
        }
        //根据商户id和商户类型去修改对应商户信息表中的状态
        if ("1".equals(merchantType)) {//个人
            ecardPersonInfoMapper.changePersonStatus(merchantId);
        }
        if ("2".equals(merchantType)) {//商户
            ecardMerchantInfoMapper.changeMerchantStatus(merchantId);
        }
        if ("3".equals(merchantType)) {//企业
            ecardEnterpriseInfoMapper.changeEnterpriseStatus(merchantId);
        }
        if ("4".equals(merchantType)) {//新型农业主体
            ecardAgricultureInfoMapper.changeAgricultureStatus(merchantId);
        }
        return ecardCustomerGroupMerchantMapper.changeIndustrialCustomerStatus(id);
    }

    /**
     * 查询行业客户地图坐标列表接口
     * @param creatorId
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiCustomerGroupMerchant> getGroupMerchantCoordinateList(Long creatorId, Long deptId) {
        return ecardCustomerGroupMerchantMapper.getGroupMerchantCoordinateList(creatorId, deptId);
    }

    /**
     * 根据法人机构ID查询机构下所有行业客户坐标信息列表
     * @param creatorId
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiCustomerGroupMerchant> getAllGroupMerchantCoordinateList(Long creatorId, Long deptId) {
        return ecardCustomerGroupMerchantMapper.getAllGroupMerchantCoordinateList(creatorId, deptId);
    }

}
